package com.example.studentmanagmentsystem;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.Operator;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //取出学生、课程与成绩信息
        List<Student> students = LitePal.findAll(Student.class);
        List<Course> courses = LitePal.findAll(Course.class);
        List<Score> scores = LitePal.findAll(Score.class);

        //查找学生及其成绩
        Button searchStudentButton = (Button) findViewById(R.id.query_student);
        EditText searchStudentInput = (EditText) findViewById(R.id.input_query);

        searchStudentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchStudentInputContent = searchStudentInput.getText().toString();
                Intent intent = new Intent(MainActivity.this, ScoreShowActivity.class);
                intent.putExtra("StudentName", searchStudentInputContent);
                startActivity(intent);
            }
        });

        //查看所有学生
        Button queryAllStudentButton = (Button) findViewById(R.id.query_all_students);
        queryAllStudentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, StudentShowActivity.class);
                startActivity(intent);
            }
        });

        //查看所有分数
        Button queryAllScoreButton = (Button) findViewById(R.id.query_all_score);
        queryAllScoreButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ScoreShowActivity.class);
                startActivity(intent);
            }
        });


        //添加学生
        Button addStudent = (Button) findViewById(R.id.add_student);
        EditText studentNameEdit = (EditText) findViewById(R.id.student_name);
        EditText studentEnterYearEdit = (EditText) findViewById(R.id.student_enter);
        RadioButton studentSexMale = (RadioButton) findViewById(R.id.sex_male_);
        RadioButton studentSexFemale = (RadioButton) findViewById(R.id.sex_female_);
        addStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studentName = studentNameEdit.getText().toString();
                int studentEnterYear = Integer.parseInt(studentEnterYearEdit.getText().toString());

                boolean studentSex = false;
                if (studentSexMale.isChecked()) {
                    studentSex = true;
                }
                if (studentName != null) {
                    Student student = new Student();
                    student.setName(studentName);
                    student.setEnterYear(studentEnterYear);
                    student.setSex(studentSex);
                    student.save();
                    Toast.makeText(MainActivity.this, "成功添加了 " + studentName, Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}